// ==++==
// 
//   Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//   License: MS-Pl
//
// ==--==

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Microsoft.Pex.Framework;
using Microsoft.Pex.Engine.ComponentModel;
using Microsoft.ExtendedReflection.Utilities.Safe.Diagnostics;
using Microsoft.Pex.Engine;
using Microsoft.Pex.Framework.Packages;

namespace Pex.NUnit.Integration
{
    [TestFixture, PexClass]
    public partial class NUnitTest
    {
        private static bool ClassInitializeTouched;
//        private static bool ClassCleanupTouched;
        private static bool TestInitializeTouched;
        private static bool TestCleanupTouched;

        [TestFixtureSetUp]
        public void ClassInitialize()
        {
            ClassInitializeTouched = true;
            Console.WriteLine("ClassInitialize");
        }

        [TestFixtureTearDown]
        public void ClassCleanup()
        {
//            ClassCleanupTouched = true;
//            Console.WriteLine("ClassCleanup");
        }

        [SetUp]
        public void TestInitialize()
        {
            TestInitializeTouched = true;
            Console.WriteLine("TestInitialize");
        }

        [TearDown]
        public void TestCleanup()
        {
            TestCleanupTouched = true;
            Console.WriteLine("TestCleanup");
        }

        [PexMethod]
        [SetUpTearDownRecorder]
        [Category("ShouldBeCopiedToGeneratedTest")]
        [Platform]
        [Explicit]
        [Description("should be copied around")]
        public void VerifyAssemblySetupTearDown()
        {
            Console.WriteLine("hello world");
        }

        [PexMethod]
        public void TestXIsOne(string x)
        {
            Assert.AreEqual("One", x);
        }

        [PexMethod]
        public void TestXIsNull(string x)
        {
            throw new ArgumentNullException(x);
        }

        private class SetUpTearDownRecorderAttribute : PexExplorationPackageAttributeBase
        {
            protected override object BeforeExploration(IPexExplorationComponent host)
            {
                return null;
            }

            protected override void AfterExploration(IPexExplorationComponent host, object data)
            {               
                SafeDebug.AssumeNotNull(host, "host");

                if (!NUnitTest.ClassInitializeTouched)
                    host.Log.LogError(null, "NUnit", "ClassInitialize was not run");
                if (!NUnitTest.TestInitializeTouched)
                    host.Log.LogError(null, "NUnit", "TestInitialize was not run");
                if (!NUnitTest.TestCleanupTouched)
                    host.Log.LogError(null, "NUnit", "TestCleanup was not run");
                host.Log.LogMessage(PexLogCategories.Execution, "setup teardown verified");
            }
        }
    }
}
